<template>
  <div class="profit">
    <div class="content">
      <div class="title">/盈利情况</div>
      <div>销售额：￥{{+(money.SalesVolume)}}</div>
      <div>成本：￥{{+(money.principalCount)}}</div>
      <div>利润：￥{{money.ShareCount+money.profit}}</div>

      <div class="list-item">
        --分销商利润：￥{{+(money.ShareCount)}}
        <div>已支付/分销商已提现：￥{{+(money.cashOut)}}</div>
        <div>未支付/分销商未提现：￥{{Subtr(money.ShareCount,money.cashOut)}}</div>
      </div>
      <div class="list-item">--商家净利润：<span class="profit-num">￥{{+(money.profit)}}</span></div>
      <div id="myChart" :style="{width: '600px', height: '400px'}"></div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Profit',
    data() {
      return {
        money:{
          SalesVolume:0,
          principalCount: 0,
          ShareCount:0,
          profit: 0
        }
      }
    },
    created() {
    },
    mounted() {
      this.getData();
    },
    methods: {
      Subtr(arg1,arg2){
        let r1,r2,m,n;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        n=(r1>=r2)?r1:r2;
        return ((arg1*m-arg2*m)/m).toFixed(n);
      },
      getData(){
        this.axios.get('/api/profit').then((res=>{
          console.log(res)
          if(res.data.errCode==0){
            this.money = res.data.result;
            this.draw();
          }
        })).catch(error=>{
          console.log(error)
        });
      },

      draw(){
        // 初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('myChart'));
        // 绘制图表
        let option = {
          series : [
            {
              name: '盈利情况',
              type: 'pie',
              radius: '55%',
              data:[
                {value:this.money.principalCount, name:'成本'},
                {value:this.money.ShareCount, name:'分销商'},
                {value:this.money.profit, name:'纯利润'}
              ],
              itemStyle: {
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                },
                normal:{
                  label:{
                    show: true,
                    formatter: '{b} : {c} ({d}%)'
                  },
                  labelLine :{show:true}
                }
              }
            }
          ]
        };
        //防止越界，重绘canvas
        window.onresize = myChart.resize;
        myChart.setOption(option);//设置option
      }
    }
  }
</script>
<style lang="less" scoped>
  .profit{
    padding: 0 20px;
    .content{
      .title{
        width: 100%;
        height: 60px;
        line-height: 60px;
      }
      div:not(.title){
        line-height: 30px;
        font-size: 14px;
        color:rgb(84, 92, 100)
      }
      .list-item{
        margin-left: 20px;
        .profit-num{
          color:rgba(227,62,51,1);
        }
        div{
          margin-left: 12px;
        }
      }
    }
  }
</style>
